package com.example.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.stu.entity.PageResult;
import com.example.stu.entity.PageStu;
import com.example.stu.entity.Result;
import com.example.stu.entity.res.StuChangePWD;
import com.example.stu.entity.res.StuSubmitVo;
import com.example.stu.mapper.StudentMapper;
import com.example.stu.pojo.Student;
import com.example.stu.service.StudentService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Date: 2021/2/24
 * @Author: 黄先军
 * @Description:
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 多条件分页查询返数据
     * @param pageStu
     * @return
     */
    @Override
    public PageResult page(PageStu pageStu) {
        Page<Student> queryPage = new Page<>(pageStu.getCurrentPage(),pageStu.getPageSize());
        //查询击中的行数
        List<Student> list = studentMapper.pages(pageStu);
        //分页后的数据
        List<Student> pages = studentMapper.pages(queryPage, pageStu);
        return new PageResult((long)list.size(), pages);
    }

    /**
     * 通过id查询学生
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getId,id));
        if(student == null){
            return new Result(false,"当前学生已被删除或修改");
        }else{
            return new Result(true,"查询学生数据成功",student);
        }
    }

    /**
     * 通过学生学号查询信息
     * @param id
     * @return
     */
    @Override
    public Result findByStuId(Integer id) {
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,id));
        if(student == null){
            return new Result(false,"当前学生已被删除或修改");
        }else{
            return new Result(true,"查询学生数据成功",student);
        }
    }

    /**
     * 新增学生数据和编辑学生数据
     * @param student
     * @return
     */
    @Override
    public Result saveOrUpdate(Student student) {
        if (student == null) {
            return new Result(false,"插入不能为空");
        }
        Integer id = student.getId();
        if(id == null){
            List<Integer> stu_id = studentMapper.selectList(new LambdaQueryWrapper<Student>()
                    .isNotNull(Student::getStuId)
                    .select(Student::getStuId))
                    .parallelStream()
                    .filter(x->x.getStuId() !=null)
                    //过滤学号是否存在，存在的话stu_id>0,不存在的话stu_id=0
                    .filter(x->x.getStuId().equals(student.getStuId()))
                    .map(Student::getStuId)
                    .collect(Collectors.toList());
            if(stu_id.size()>0 && stu_id.size()!=0){
                return new Result(false,"学号已存在，不能添加该学号数据");
            }
            //设置学生的密码为身份证后6位
            student.setStuPwd(student.getStuIdCard().substring(student.getStuIdCard().length()-6));
            studentMapper.insert(student);
            return new Result(true,"新加学生数据成功");
        }else{
            studentMapper.updateById(student);
            return new Result(true,"修改学生数据成功");
        }
    }

    /**
     * 逻辑删除学生
     * @param id
     * @return
     */
    @Override
    public Result remove(Integer id) {
        Student student = studentMapper.selectById(id);
        if(student == null){
            return new Result(false,"当前学生已被删除,不能继续操作");
        }
        student.setDelFlag(true);
        studentMapper.updateById(student);
        return  new Result(true,"删除学生数据成功");
    }

    /**
     * 学生修改提交自己的信息
     * @param stuSubmitVo
     * @return
     */
    @Override
    public Result tijiaoxinxi(StuSubmitVo stuSubmitVo) {
        Object a = (String) redisTemplate.opsForValue().get(stuSubmitVo.getStuMail());
        if(StringUtils.isNullOrEmpty(stuSubmitVo.getYzm())){
            return new Result(false,"验证码不能为空");
        }
        if(!stuSubmitVo.getYzm().equals(a)){
            return new Result(false,"验证码错误");
        }
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,stuSubmitVo.getStuId()));
        student.setStuMail(stuSubmitVo.getStuMail());
        student.setStuPhone(stuSubmitVo.getStuPhone());
        student.setStuOneCard(stuSubmitVo.getStuOneCard());
        student.setStuBedding(stuSubmitVo.getStuBedding());
        student.setStuTrainUniform(stuSubmitVo.getStuTrainUniform());
        student.setStuDormitory(stuSubmitVo.getStuDormitory());
        Result result = this.saveOrUpdate(student);
        if(result.isFlag()){
            return new Result(true,"提交信息成功");
        }else{
            return new Result(false,"提交信息失败");
        }
    }

    /**
     * 学生到校签到
     * @param stuSubmitVo
     * @return
     */
    @Override
    public Result signIn(StuSubmitVo stuSubmitVo) {
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,stuSubmitVo.getStuId()));
        if(student.getStuInSchool()!=null){
            return new Result(false,"你已经签到，请勿重复提交");
        }
        student.setStuInSchool(new Date());
        Result result = this.saveOrUpdate(student);
        if(result.isFlag()){
            return new Result(true,"签到成功");
        }else{
            return new Result(false,"签到失败");
        }
    }

    /**
     * 学生修改密码
     * @param stuChangePWD
     * @return
     */
    @Override
    public Result stuChangePWD(StuChangePWD stuChangePWD) {
        if(stuChangePWD == null){
            return new Result(false,"参数错误");
        }
        if(StringUtils.isNullOrEmpty(stuChangePWD.getLastpassword())){
            return new Result(false,"旧密码不能为空");
        }
        if(StringUtils.isNullOrEmpty(stuChangePWD.getNewpassword())){
            return new Result(false,"新密码不能为空");
        }
        if(StringUtils.isNullOrEmpty(stuChangePWD.getYzm())){
            return new Result(false,"验证码不能为空");
        }
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>().eq(Student::getStuId,stuChangePWD.getStuId()));
        Object a = (String) redisTemplate.opsForValue().get(student.getStuMail());
        if(!stuChangePWD.getYzm().equals(a)){
            return new Result(false,"验证码错误");
        }
        student.setStuPwd(stuChangePWD.getNewpassword());
        this.saveOrUpdate(student);
        return new Result(true,"修改密码成功");
    }

    /**
     * 查询到校的学生人数
     * @return
     * @param majorId
     */
    @Override
    public Map<String,Integer> findStuInSchool(String majorId) {
        if(majorId.equals("99")){
            Integer all = studentMapper.selectCount(new LambdaQueryWrapper<Student>().isNotNull(Student::getStuId));
            Integer inschool = studentMapper.selectCount(new LambdaQueryWrapper<Student>().isNotNull(Student::getStuInSchool));
            Map<String,Integer> map = new HashMap<>(2);
            map.put("All",all);
            map.put("inSchool",inschool);
            return map;
        }else{
            Integer all = studentMapper.selectCount(new LambdaQueryWrapper<Student>().eq(Student::getMajorId,majorId).isNotNull(Student::getStuId));
            Integer inschool = studentMapper.selectCount(new LambdaQueryWrapper<Student>().eq(Student::getMajorId,majorId).isNotNull(Student::getStuInSchool));
            Map<String,Integer> map = new HashMap<>(2);
            map.put("All",all);
            map.put("inSchool",inschool);
            return map;
        }
    }


}
